package com.mm.googleauth.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mm.googleauth.service.DingdingService;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DingdingServiceImpl implements DingdingService {

    @Value("${dd.app-key}")
    private String key;

    @Value("${dd.app-secret}")
    private String secret;

    @Value("${dd.server-url}")
    private String serverUrl;

    private Logger log = LoggerFactory.getLogger(DingdingServiceImpl.class);

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public String accessToken(String userId) {
        if (userId == null) {
            DefaultDingTalkClient client = new DefaultDingTalkClient(serverUrl+"/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.setAppkey(key);
            request.setAppsecret(secret);
            request.setHttpMethod("GET");
            log.info("key={}, secret={}", key, secret);
            try {
                OapiGettokenResponse response = client.execute(request);
                String token = response.getAccessToken();
                log.info("生成的access-token: {}", token);
                return token;
            } catch (ApiException e) {
                log.error("access-token: {}", e);
                return null;
            }
        }

        return null;
    }

    @Override
    public String userId(String code) {
        String token = accessToken(null);
        DingTalkClient client = new DefaultDingTalkClient(serverUrl+"/user/getuserinfo");
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(code);
        request.setHttpMethod("GET");
        OapiUserGetuserinfoResponse response = null;
        try {
            response = client.execute(request, token);
            String userId = response.getUserid();
            return userId;
        } catch (ApiException e) {
            log.error("获取用户id异常:{}", e);
        }

        return null;
    }

    public OapiUserGetResponse userInfo(String userId) {
        DingTalkClient client = new DefaultDingTalkClient(serverUrl+"/user/get");
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid(userId);
        request.setHttpMethod("GET");

        String token = accessToken(null);
        try {
            OapiUserGetResponse response = client.execute(request, token);
            String err = response.getErrorCode();
//            if (err != null) {
//                log.error("用户信息接口返回失败: {}", err);
//                return null;
//            }
            log.info("请求google-auth-code用户信息: {}", mapper.writeValueAsString(response));
            return response;
        } catch (ApiException | JsonProcessingException e) {
            log.error("获取用户信息失败: {}", e);
        }
        return null;
    }
}
